package org.example.device.registry;

import org.example.device.protocol.DeviceProtocol;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备协议注册中心
 * 管理不同通信协议的实现
 */
@Component
public class DeviceProtocolRegistry {
    
    @Autowired
    private List<DeviceProtocol> deviceProtocols;
    
    private Map<String, DeviceProtocol> protocolMap = new HashMap<>();
    
    @PostConstruct
    public void init() {
        // 注册所有设备协议
        for (DeviceProtocol protocol : deviceProtocols) {
            String protocolType = protocol.getProtocolType();
            protocolMap.put(protocolType, protocol);
            System.out.println("注册设备协议: " + protocolType);
        }
    }
    
    /**
     * 根据协议类型获取协议实现
     */
    public DeviceProtocol getProtocol(String protocolType) {
        return protocolMap.get(protocolType);
    }
    
    /**
     * 获取所有支持的协议类型
     */
    public List<String> getSupportedProtocolTypes() {
        return List.copyOf(protocolMap.keySet());
    }
    
    /**
     * 检查是否支持指定协议类型
     */
    public boolean isSupported(String protocolType) {
        return protocolMap.containsKey(protocolType);
    }
    
    /**
     * 获取所有协议
     */
    public Map<String, DeviceProtocol> getAllProtocols() {
        return new HashMap<>(protocolMap);
    }
}
